import {Component, OnInit, ViewEncapsulation, ChangeDetectionStrategy, OnDestroy} from '@angular/core';
import {ActivatedRoute, Params, Router} from "@angular/router";
import {SaleService} from "@sale/sale.service";
import {Subject} from "rxjs/Subject";
import {AuthService} from "@shared/service/auth/auth.service";
import {TrainSaleSignFormRespVo} from "@interface/index";


@Component({
  selector: 'seed-add-trial-campus',
  templateUrl: './add-trial-campus.component.html',
  styleUrls: ['./add-trial-campus.component.scss'],
  encapsulation: ViewEncapsulation.None,
})
export class AddTrialCampusComponent implements OnInit, OnDestroy {
  public orgUserId: string;
  public courseId: string;
  public studentId: string;
  public saleUserId: string;
  public allData: Array<TrainSaleSignFormRespVo>;
  public ngUnsubscribe: Subject<void> = new Subject<void>();
  public campusInfo = [];

  constructor(
    public route: ActivatedRoute,
    public saleService: SaleService,
    public router: Router,
    private authService: AuthService
  ) {
    this.orgUserId = this.authService.getAuthOrgId();
    this.route.params.subscribe((param: Params) => {
      this.courseId = param['courseId'];
      // console.log(this.courseId);
      this.studentId = param['studentId'];
      this.saleUserId = param['saleUserId'];
      this.saleService.getCourseList(this.orgUserId, 2).takeUntil(this.ngUnsubscribe).subscribe(res => {
          if (res.result === '0') {
            this.allData = res.data;
            // console.log(res.data);
            for (let key in this.allData) {
              if (this.allData[key].trainCourseSimpleVO.id === parseInt(this.courseId, 10)) {
                this.campusInfo = [...this.campusInfo, ...this.allData[key].simpleVOList];
              }
            }
          }
        }
      );
    });
  }

  goToClass(schoolId) {
    this.router.navigate([`/sale/class/${this.studentId}/${this.saleUserId}/${this.courseId}/${schoolId}`]);
  }

  ngOnInit() {

  }

  ngOnDestroy() {
    this.ngUnsubscribe.next();
    this.ngUnsubscribe.complete();
  }

}
